// 音频
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:just_audio/just_audio.dart';

class XinDongAudioCtr extends GetxController {
  static XinDongAudioCtr get to => Get.find();

  final _audioPlayer = AudioPlayer();
  bool isPlaying = false;
  String playUrl = "";

  @override
  void onInit() {
    super.onInit();
    _audioPlayer.playerStateStream.listen((state) {
      isPlaying = state.playing && state.processingState != ProcessingState.completed;
      update();
      // switch (state.processingState) {
      // case ProcessingState.idle: ...
      // case ProcessingState.loading: ...
      // case ProcessingState.buffering: ...
      // case ProcessingState.ready: ...
      // case ProcessingState.completed: ...
      // }
    });
  }

  void clear() {
    playUrl = "";
    isPlaying = false;
    _audioPlayer.stop();
    update();
  }

  void play(String url) {
    if (url.trim().isEmpty) {
      debugPrint("地址为空");
      return;
    }
    playUrl = url;
    _audioPlayer.setUrl(playUrl);
    _audioPlayer.setLoopMode(LoopMode.off);
    _audioPlayer.play();
  }
}
